package com.lsfb.light;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.lsfb.light.Index.IndexActivity;
import com.lsfb.light.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.LsfbAppManager;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;

public class StartActivity extends Activity {
    private static String pushid;
    public static boolean isForeground = false;
    private EditText msgText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_guide);


        registerMessageReceiver();  // used for receive msg

        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Config.setScreen(true);
        Config.vertica = true;
        Config.Config(this);
//        pushid = JPushInterface.getRegistrationID(this);
//        LsfbLog.e("jpushid:" + JPushInterface.getRegistrationID(this));
        //音效设置初始化，默认开启
//        if (SPUtils.get(this, "sound", "").toString().isEmpty()) {
//            SPUtils.put(this, "sound", "1");
//            Constant.IsSound = true;
//        } else if (SPUtils.get(this, "sound", "").toString().equals("1")) {
//            Constant.IsSound = true;
//        } else if (SPUtils.get(this, "sound", "").toString().equals("0")) {
//            Constant.IsSound = false;
//        }
//        String fnumber = (String) SPUtils.get(this, Constant.KEY_FNUMBER, "");
//        if (!fnumber.isEmpty()) {
//            MyApplication.fnumber = fnumber;
//        }
        Config.ConfigBaseURL(URLString.BASE_URL);
        Config.TOKENTYPE = 2;
        Config.showTitlebarLine = false;
        Config.Style_AppColor = this.getResources().getColor(R.color.white_color);
        LsfbAppManager.getAppManager().addActivity(this);
        BaseInternet.setmContext(getApplicationContext());


        //启动页
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                enterHomeActivity();
            }
        }, 2000);
    }

    private void enterHomeActivity() {
        startActivity(new Intent(this, IndexActivity.class));
        finish();
    }


    @Override
    protected void onResume() {
        isForeground = true;
        super.onResume();
    }


    @Override
    protected void onPause() {
        isForeground = false;
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
        super.onDestroy();
    }

    /**
     * 屏蔽物理返回按钮
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //for receive customer msg from jpush server
    private MessageReceiver mMessageReceiver;
    public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION";
    public static final String KEY_TITLE = "title";
    public static final String KEY_MESSAGE = "message";
    public static final String KEY_EXTRAS = "extras";

    public void registerMessageReceiver() {
        mMessageReceiver = new MessageReceiver();
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(MESSAGE_RECEIVED_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, filter);
    }

    public class MessageReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) {
                    String messge = intent.getStringExtra(KEY_MESSAGE);
                    String extras = intent.getStringExtra(KEY_EXTRAS);
                    StringBuilder showMsg = new StringBuilder();
                    showMsg.append(KEY_MESSAGE + " : " + messge + "\n");
                    //todo 极光工具  不知道干啥的，先注释了
//                    if (!JPushUtil.isEmpty(extras)) {
//                        showMsg.append(KEY_EXTRAS + " : " + extras + "\n");
//                    }
                    setCostomMsg(showMsg.toString());
                }
            } catch (Exception e) {
            }
        }
    }

    private void setCostomMsg(String msg) {
        if (null != msgText) {
            msgText.setText(msg);
            msgText.setVisibility(View.VISIBLE);
        }
    }

}
